<?php

namespace App\Http\Controllers\Common\English;

use App\Models\Common\English\EnCollPosition;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Common\CommonController;
use Cache;

class EnPositionController extends CommonController
{
    /**
     * @return \Illuminate\Http\JsonResponse
     * 说明:打印一级职位json//一维
     */
    public function firstPosition(){
        $positions=EnCollPosition::getFirstPosition();
        return response()->json($positions);
    }

    /**
     * @param $cid
     * @return \Illuminate\Http\JsonResponse
     * 说明:打印二级职位json//一维
     */
    public function secondPosition($cid){
        $positions=EnCollPosition::getSecondPosition($cid);
        return response()->json($positions);
    }

    /**
     * @param $cid
     * @return \Illuminate\Http\JsonResponse
     * 说明:打印三级职位json//一维
     */
    public function thirdPosition($cid){
        $positions=EnCollPosition::getThirdPosition($cid);
        return response()->json($positions);
    }


    /**
     * @return \Illuminate\Http\JsonResponse
     * 说明：打印一、二级职位二维json
     */
    public function allTwo(){
        $positions=EnCollPosition::getAllTwo();
        return response()->json($positions);
    }


    /**
     * @return \Illuminate\Http\JsonResponse
     * 说明：打印一、二、三级职位三维json
     */
    public function allThree(){
        $positions=EnCollPosition::getAllThree();
        return response()->json($positions);
    }


    /**
     * @return \Illuminate\Http\JsonResponse
     * 说明：搜索职位三级
     */
    public function searchThree($keyword){
        $positionArr=EnCollPosition::getSearchThree($keyword);
        return response()->json($positionArr);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 说明：获取一级职位json例表js
     */
    public function onePosition(){
        if (Cache::has('onePosition')) {
            $value = Cache::get('onePosition');
        }else{
            $jsstr='';$arr=array();
            //获取一级职位列表
            $arr['position']=EnCollPosition::getFirstPosition();
            foreach($arr as $k=>$v){
                $option=json_encode($v);
                if($option!='[]') $jsstr.='var option_'.$k.'='.$option.';';//把json转成字符串拼接万js代码
            }
            $value= $jsstr?$jsstr:'';
            if($value) Cache::forever('onePosition', $value);
        }
        return $value?$value:"var error_option= '没有找到对应的选项!';";
    }


    /**
     * @return \Illuminate\Http\JsonResponse
     * 说明：单个获取当前职位
     */
    public function currPosition($cid){
        return EnCollPosition::getPosName($cid);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 说明：多级选项获取当前职位名称
     */
    public function getPositionName($arr){
        $areaName =  array();
        $arr = explode(',',$arr);
        if(count($arr)){
            foreach($arr as $v){
                $areaName[]=array('value'=>$v,'text'=>$this->currPosition($v));
            }
        }
        return response()->json($areaName);
    }
}
